Next | Prev | Up | Top | Contents | Index

Header Files

Header files, also called include files, contain information about the libraries with which they're associated. They define such things as data types, data structures, symbolic constants, and prototypes for functions exported by the library. To use those definitions without having to type them into each of your source files, you can use the #include directive to tell the macro preprocessor to include the complete text of the given header file in the current source file. When you include header files in your source files you can specify such definitions conveniently and consistently in each source file that uses any of the library routines.

By convention, header filenames have a .h suffix. Each programming language handles these files the same way, via the macro preprocessor. For example, the stdio.h header file describes, among other things, the data types of the parameters required by printf().

For detailed information about standard header files and libraries, see the International Standard ISO/IEC. Programming languages--C, 9899. 1990. Also see "Using Typedefs" for information about the inttypes.h header file.


Specifying a Header File

The #include directive tells the preprocessor to replace the #include line with the text of the indicated header file. The usual way to specify a header file is with the line

#include <filename>

where filename is the name of the header file to be included. The angle brackets (< >) surrounding the filename tell the macro preprocessor to search for the specified file only in directories specified by command-line options and in the default header-file directory (/usr/include).

Another specification format exists, in which the filename is given between double quotation marks. In this case, the macro preprocessor searches for the specified header file in the current directory first (that is, the directory containing the including file). Then, if the preprocessor doesn't find the requested file, it searches in the other directories as in the angle-bracket specification.


Creating a Header File for Multiple Languages

A single header file can contain definitions for multiple languages; this setup allows you to use the same header file for all programs that use a given library, no matter what language those programs are in.

To set up a shareable header file, create a .h file and enter the definitions for the various languages as follows:

#ifdef _LANGUAGE_C

C definitions

#endif

#ifdef _LANGUAGE_C_PLUS_PLUS

C++ definitions

#endif

#ifdef _LANGUAGE_FORTRAN

Fortran definitions

#endif

Note: You must specify _LANGUAGE_ before the language name. To indicate C++ definitions, you must use _LANGUAGE_C_PLUS_PLUS, not _LANGUAGE_C++. You can specify the various language definitions in any order.


Next | Prev | Up | Top | Contents | Index